/**
 * 数据字典工具类
 */
import { useCommonStore } from '@/store/modules/common'

export const getDictOptions = (dictType: string) => {
  const commonStore = useCommonStore()
  const wordbook = commonStore.wordbook
  const dictOptions = wordbook[dictType]
  if (!dictOptions) {
    console.error('缺少字典字段：' + dictType)
    return []
  }
  return dictOptions
}

/**
 * 获得字典数据的文本展示 即Label
 *
 * @param dictType 字典类型
 * @param value 字典数据的值
 * @return 字典名称
 */
export const getDictLabel = (dictType: string, value: any): string => {
  const commonStore = useCommonStore()
  const wordbook = commonStore.wordbook
  const dictOptions = wordbook[dictType]
  if (!dictOptions) {
    console.error('缺少字典字段：' + dictType)
    return ''
  }
  const dictLabel = ref('')
  dictOptions.forEach((dict: any) => {
    if (dict.dictValue === value) {
      dictLabel.value = dict.dictLabel
    }
  })
  return dictLabel.value
}
